package cl.falabella.cmr.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cl.falabella.cmr.dao.RegionDAO;

public class RegionesServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//req.getRequestDispatcher("/WEB-INF/html/region-search.html").forward(req, resp);
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		List<RegionDAO> regiones = RegionDAO.getRegiones();
		List<RegionDAO> regionesFiltradas = new ArrayList<RegionDAO>();

		String elPatron = req.getParameter("patron");

		if (elPatron == null) {
			regionesFiltradas = regiones;
		} else {
			for (RegionDAO region : regiones) {
				if (region.getNombre().contains(elPatron)) {
					regionesFiltradas.add(region);
				}
			}
		}
		req.setAttribute("regiones", regionesFiltradas);
		// req.getRequestDispatcher("/WEB-INF/jsp/Regiones.jsp").forward(req,
		// resp);
		req.getRequestDispatcher("/WEB-INF/jsp/RegionesJSTL.jsp").forward(req,
				resp);
	}

}
